package com.captjack.fastdfs.handle.impl;

import com.captjack.fastdfs.handle.FastDistributeFileSystemHandle;
import org.apache.commons.io.IOUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.util.Properties;
import java.util.UUID;

/**
 * @author Capt Jack
 * @date 2018/6/28
 */
@Component
public class FastDistributeFileSystemHandleImpl implements FastDistributeFileSystemHandle {

    /**
     *
     */
    private TrackerClient trackerClient;

    /**
     *
     */
    private TrackerServer trackerServer;

    /**
     *
     */
    private StorageClient1 storageClient1;

    public void download() throws Exception {
        byte[] b = storageClient1.download_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");
        System.out.println(b);
        IOUtils.write(b, new FileOutputStream("D:/" + UUID.randomUUID().toString() + ".conf"));
    }

    /**
     * 加载文件系统配置文件，初始化对象
     *
     * @throws Exception 异常
     */
    @Autowired
    public FastDistributeFileSystemHandleImpl(@Qualifier(value = "fastDistributeFileSystemConfigurationProperties") Properties properties) throws Exception {
        // 初始化系统信息
        ClientGlobal.initByProperties(properties);
        // trackerClient
        trackerClient = new TrackerClient();
        // trackerServer
        trackerServer = trackerClient.getConnection();
        // storageClient1
        storageClient1 = new StorageClient1(trackerServer, null);
    }

}
